package org.lifei.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.lifei.base.annotation.LifPermission;
import org.lifei.base.util.AjaxResult;
import org.lifei.base.util.PageInfo;
import org.lifei.org.domain.Department;
import org.lifei.org.query.Departmentquery;
import org.lifei.org.service.impl.DepartmentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController//以后后台只会返回数据  所以我们使用RestCOntrller
@RequestMapping("/dep")
@Api(value = "部门管理接口",description = "部门接口")
@LifPermission(name = "部门管理" ,desc = "部门相关接口")
public class DepartmentController {
    @Autowired
    private DepartmentServiceImpl service;
    /**
     * RestFul请求方式  通过请求方式来控制访问接口
     */
    @PutMapping
    @ApiOperation(value = "部门新增/修改")
    @LifPermission(name = "部门保存",desc = "部门新增/修改")
    public AjaxResult save(@RequestBody Department department){
        try {
            if (Objects.isNull(department.getId())){
                service.insert(department);
            }else {
                service.update(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @DeleteMapping({"/{id}"})
    @ApiOperation(value = "删除指定部门")
    @LifPermission(name = "删除指定部门",desc = "删除指定部门")
    public AjaxResult deleteById(@PathVariable("id")Long id){
        try {
            service.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @GetMapping
    @ApiOperation(value = "查询所有部门")
    @LifPermission(name = "查询所有部门",desc = "查询所有部门")
    public AjaxResult queryAll(){
        try {
            List<Department> departments = service.queryAll();
            return AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "查询指定部门")
    @LifPermission(name = "查询指定部门",desc = "根据id查询部门")
    public AjaxResult queryOne(@PathVariable("id")Long id){
        try {
            Department department = service.queryOne(id);
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @PatchMapping
    @ApiOperation(value = "批量删除部门")
    @LifPermission(name = "批量删除部门",desc = "批量删除部门")
    public AjaxResult batchDelete(@RequestBody List<String> ids){
        try {
            service.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @PostMapping
    @ApiOperation(value = "分页查询部门")
    @LifPermission(name = "分页查询部门",desc = "分页查询部门")
    public AjaxResult queryPage(@RequestBody Departmentquery departmentquery){
        try {
            PageInfo<Department> departments=service.queryPage(departmentquery);
            return AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/depTree")
    @ApiOperation(value = "查询部门树")
    @LifPermission(name = "查询部门树",desc = "查询部门树")
    public AjaxResult DepTree(){
        try {
            List<Department> DepTree = service.DepTree();
            return AjaxResult.success(DepTree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
